local Class = require('Core.Class')

if IS_EDITOR then
    require("Debugger.LuaPanda").start("127.0.0.1", 8818)
end

-- @Class EventManager
EventManager = Class('EventManager')

function EventManager:__init()
    self.eventTable = {}
end

-- 侦听事件
function EventManager:AddListener(eventName, funCallback)
    assert(funCallback)
    if self.eventTable[eventName] == nil then
        self.eventTable[eventName] = {}
    else
        self.eventTable[eventName] = self.eventTable[eventName]
    end
    local event = self.eventTable[eventName]
    table.insert(event, funCallback)
end

-- 移除事件
function EventManager:RemoveListener(eventName, funCallback)
    if eventName ~= nil then
        if self.eventTable[eventName] ~= nil then
            for i = #self.eventTable[eventName], 1, -1 do
                if(self.eventTable[eventName][i] == funCallback) then
                    table.remove(self.eventTable[eventName], i)
                end
            end
        end
    end
end

function EventManager:RemoveAllListener(eventName)
    if eventName ~= nil then
        if self.eventTable[eventName] ~= nil then
            self.eventTable[eventName] = nil
        end
    end
end

-- 发布事件
function EventManager:Fire(eventName, ...)
    if eventName ~= nil then
        print('触发<color=#00FF00>'..eventName..'</color>事件')
        if self.eventTable[eventName] ~= nil then
            for _, funCallback in pairs(self.eventTable[eventName]) do
LuaPanda.BP()
                funCallback(...)
            end
        end
    end
end

return EventManager.New()

